import java.net.*;
import java.io.*;
import java.util.*;

public class DummyClient {
  private ChatClientRecord clientRec;

  public DummyClient(String serverName, int serverPort, String clientName) {
    Socket sock = null;
    ObjectOutputStream out = null;
    ObjectInputStream in = null;

    // Attempt to connect to server
    try {
      sock = new Socket(serverName, serverPort);
      out = new ObjectOutputStream(sock.getOutputStream());
      in = new ObjectInputStream(sock.getInputStream());
    } catch (IOException e) {
      System.err.println("Error: "+e.getMessage());
    }

    // Send dummy registration request
    this.clientRec = new ChatClientRecord(clientName, "", 0);
    ChatMessage regRequest = new ChatMessage(ChatMessage.REG_ATTEMPT, null, null, clientRec);
    try {
      out.writeObject(regRequest);
    } catch (IOException e) {
      System.err.println("Error: "+e.getMessage());
    }
  }

  public static void main(String[] args){
    if(args.length == 2){
      String thishost = "";
      try{
        thishost = InetAddress.getLocalHost().getHostName();
      }catch(UnknownHostException e){
        System.err.println(e.getMessage());
        System.exit(1);
      }
      long start = System.currentTimeMillis();
      long latency_start = 0;
      for(int i = 0; i < 100000; i++){
        long latency_tmp = System.currentTimeMillis();
        try{
        new DummyClient(args[0],Integer.parseInt(args[1]),thishost+i);
        }catch(Exception e){
            /* Empty to make sure any unforseen exception
             * doesn't ruin all the data*/
        }
        latency_start += System.currentTimeMillis() - latency_tmp;
      }
      double throughput = (100000*1000) / (double)(System.currentTimeMillis() - start);
      double latency = (double)latency_start / (100000*1000);
      System.out.println("Throughput: " + throughput);
      System.out.println("Latency: " + latency);
    }else{
      System.err.println("Usage: java DummyClient <serverName> <serverPort>");
      System.exit(1);
    }
  }
}
